package com.lemon.exam.common.enums;

import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;

/**
 * 状态码枚举类
 *
 * @author Lemon
 * @since 2025/3/22 11:18
 */
@Getter
@AllArgsConstructor
public enum CodeEnum {
    SUCCESS(200, "操作成功！"),
    ERROR_400(400, "操作失败！"),
    ERROR_401(401, "无操作权限！"),
    ERROR_402(402, "请求参数错误！"),
    ERROR_403(403, "认证错误！"),
    ERROR_404(404, "请求方法不存在！"),
    ERROR_405(405, "请求方法不正确！"),
    ERROR_406(406, "请勿重复操作，请稍后再试！"),
    ERROR_407(407, "异地登录！"),
    ERROR_408(408, "请求超时！"),
    ERROR_409(409, "请求参数错误！"),
    ERROR_412(412, "参数传递异常！"),
    ERROR_413(413, "请求实体过大！"),
    ERROR_414(414, "请求URI过长！"),
    ERROR_415(415, "请求格式不正确！"),
    ERROR_416(416, "请求范围不符合要求！"),
    ERROR_418(418, "请勿重复提交！"),
    ERROR_500(500, "服务器异常！"),
    ERROR_501(501, "服务不可用！"),
    ERROR_502(502, "网关错误！"),
    ERROR_503(503, "服务不可用！"),
    ERROR_504(504, "网关超时！"),
    ERROR_505(505, "HTTP版本不受支持！"),
    ERROR_506(506, "代理错误！"),
    ERROR_507(507, "服务器无法存储请求数据！"),
    ERROR_508(508, "服务器无法处理请求！"),

    /**
     * 自定义状态码枚举
     */
    TOKEN_REFRESH(4001, "刷新TOKEN"),

    ;

    private final int code;
    private final String message;

    /**
     * 根据message获取枚举
     *
     * @param message
     * @return
     */
    public static CodeEnum getByMessage(String message) {
        return Arrays.stream(values())
                .filter(codeEnum -> codeEnum.getMessage().equals(message))
                .findFirst()
                .orElse(null);
    }

    /**
     * 根据code获取枚举
     *
     * @param code
     * @return
     */
    public static CodeEnum getByCode(int code) {
        return Arrays.stream(values())
                .filter(codeEnum -> codeEnum.getCode() == code)
                .findFirst()
                .orElse(null);
    }
}
